#!/usr/bin/env python
# -*- coding:utf-8 -*-
# Author:Zhangcl

from django.forms import ModelForm
from crm import models


class EnrollmentForm(ModelForm):  # 创建报名表发起表单

    def __new__(cls, *args, **kwargs):
        for field_name, field_obj in cls.base_fields.items():
            field_obj.widget.attrs['class'] = 'form-control'
        return ModelForm.__new__(cls)

    class Meta:
        model = models.Enrollment
        fields = ["enrolled_class", "consultant"]


class CustomerForm(ModelForm):  # 创建报名表单

    def __new__(cls, *args, **kwargs):
        for field_name, field_obj in cls.base_fields.items():
            field_obj.widget.attrs['class'] = 'form-control'
            if field_name in cls.Meta.readonly_fields:
                field_obj.widget.attrs['disabled'] = 'disabled'
        return ModelForm.__new__(cls)

    def clean_qq(self):
        if self.instance.qq != self.cleaned_data['qq']:
            self.add_error("qq", "请勿修改只读数据！")
        return self.instance.qq

    def clean_source(self):
        if self.instance.source != self.cleaned_data['source']:
            self.add_error("source", "请勿修改只读数据！")
        return self.instance.source

    def clean_consultant(self):
        if self.instance.consultant != self.cleaned_data['consultant']:
            self.add_error("consultant", "请勿修改只读数据！")
        return self.instance.consultant

    class Meta:
        model = models.Customer
        fields = "__all__"
        exclude = ['tags', 'content', 'memo', 'status', 'referral_from', 'consult_course']
        readonly_fields = ['qq', 'consultant', 'source', ]

#
# class PaymentForm(ModelForm):  # 创建缴费表单
#
#     def __new__(cls, *args, **kwargs):
#         for field_name, field_obj in cls.base_fields.items():
#             field_obj.widget.attrs['class'] = 'form-control'
#         return ModelForm.__new__(cls)
#
#     class Meta:
#         model = models.Payment
#         fields = "__all__"
